package com.retail.bbq.cola.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.retail.bbq.cola.constant.RedisConstant;
import com.retail.bbq.cola.entity.StampCollectionExpirationNotice;
import com.retail.bbq.cola.entity.WechatCustomer;
import com.retail.bbq.cola.enums.ErrorCodeEnum;
import com.retail.bbq.cola.exception.BusinessException;
import com.retail.bbq.cola.service.StampCollectionExpirationNoticeService;
import com.retail.bbq.cola.mapper.StampCollectionExpirationNoticeMapper;
import com.retail.bbq.cola.service.WechatCustomerService;
import com.retail.bbq.cola.vo.request.stampCollection.LawsonStampCollectionRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
* @author jarvis.zhang
* @description 针对表【stamp_collection_expiration_notice(集章活动过期提醒表)】的数据库操作Service实现
* @createDate 2025-07-26 17:58:02
*/
@Service
@Slf4j
public class StampCollectionExpirationNoticeServiceImpl extends ServiceImpl<StampCollectionExpirationNoticeMapper, StampCollectionExpirationNotice>
    implements StampCollectionExpirationNoticeService{

    @Resource
    private RedisTemplate redisTemplate;
    @Resource
    private WechatCustomerService wechatCustomerService;

    @Override
    public Boolean isExists(LawsonStampCollectionRequest request) {
        //返回 true:有记录，代表已通知过
        WechatCustomer userInfo = wechatCustomerService.getUserInfo(request.getOneid());
        if(Objects.isNull(userInfo)){
            //当前用户不存在的话  不提示
            return Boolean.TRUE;
        }
        String redisKey = String.format(RedisConstant.REDIS_KEY_STAMP_COLLECTION_EXPIRATION_NOTICE,
                request.getSetupId(), request.getMonth(), userInfo.getOneid());
        log.info("isExists:{}", redisKey);
        //先查缓存，再查数据库
        Object colaAnswerRecordListCacheInfo = redisTemplate.opsForValue().get(redisKey);
        log.info("isExists:{},result:{}", redisKey, colaAnswerRecordListCacheInfo);
        if(Objects.nonNull(colaAnswerRecordListCacheInfo)){
            return Boolean.TRUE;
        }
        StampCollectionExpirationNotice stampCollectionExpirationNotice = baseMapper.selectOne(new LambdaQueryWrapper<StampCollectionExpirationNotice>()
                .eq(StampCollectionExpirationNotice::getSetupId, request.getSetupId())
                .eq(StampCollectionExpirationNotice::getMonth, request.getMonth())
                .eq(StampCollectionExpirationNotice::getOneid, userInfo.getOneid())
                .last("limit 1"));
        if(Objects.nonNull(stampCollectionExpirationNotice)){
            redisTemplate.opsForValue().set(redisKey, "1", 30, TimeUnit.MINUTES);
            return Boolean.TRUE;
        }
        stampCollectionExpirationNotice = new StampCollectionExpirationNotice();
        stampCollectionExpirationNotice.setSetupId(request.getSetupId());
        stampCollectionExpirationNotice.setMonth(request.getMonth());
        stampCollectionExpirationNotice.setOneid(userInfo.getOneid());
        stampCollectionExpirationNotice.setNoticeTime(new Date());
        this.save(stampCollectionExpirationNotice);
        //没有的话，就插入数据库，更新缓存
        redisTemplate.opsForValue().set(redisKey, "1", 30, TimeUnit.MINUTES);
        return Boolean.FALSE;
    }
}




